/**
 * Created by jf on 16/1/14.
 */
class StringUtil {
    static replaceAll(s, s1, s2) {
        return s.replace(new RegExp(s1, "gm"), s2);
    }
}

class ArrayUtil {
    static delete(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }
}

class PhoneUtil {
    static checkPhone(phonenum) {
        if (phonenum.length == 11) {
            return false;
        } else {
            return true;
        }
    }
}


/**
 * 银行卡校验
 */
class BackCardUtil {
    static checkBankCard(bankid) {
        let bankno = bankid.replace(/\s/g, '');
        if (bankno == "") {
            // layer.msg("请填写银行卡号");
            return false;
        }
        if (bankno.length < 16 || bankno.length > 19) {
            // layer.msg("银行卡号长度必须在16到19之间");
            return false;
        }
        let num = /^\d*$/;//全数字
        if (!num.exec(bankno)) {
            // layer.msg("银行卡号必须全为数字");
            return false;
        }
        //开头6位
        let strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
        if (strBin.indexOf(bankno.substring(0, 2)) == -1) {
            // layer.msg("银行卡号开头6位不符合规范");
            return false;
        }
        //Luhn校验
        if (!this.luhnCheck(bankno)) {
            return false;
        } else {
            return true;
        }
    }

    static luhnCheck(bankno) {
        let lastNum = bankno.substr(bankno.length - 1, 1);//取出最后一位（与luhn进行比较）
        let first15Num = bankno.substr(0, bankno.length - 1);//前15或18位
        let newArr = [];
        for (let i = first15Num.length - 1; i > -1; i--) {    //前15或18位倒序存进数组
            newArr.push(first15Num.substr(i, 1));
        }
        let arrJiShu = [];  //奇数位*2的积 <9
        let arrJiShu2 = []; //奇数位*2的积 >9
        let arrOuShu = [];  //偶数位数组
        for (let j = 0; j < newArr.length; j++) {
            if ((j + 1) % 2 == 1) {//奇数位
                if (parseInt(newArr[j]) * 2 < 9)
                    arrJiShu.push(parseInt(newArr[j]) * 2);
                else
                    arrJiShu2.push(parseInt(newArr[j]) * 2);
            }
            else //偶数位
                arrOuShu.push(newArr[j]);
        }
        let jishu_child1 = [];//奇数位*2 >9 的分割之后的数组个位数
        let jishu_child2 = [];//奇数位*2 >9 的分割之后的数组十位数
        for (let h = 0; h < arrJiShu2.length; h++) {
            jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
            jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
        }

        let sumJiShu = 0; //奇数位*2 < 9 的数组之和
        let sumOuShu = 0; //偶数位数组之和
        let sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
        let sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
        let sumTotal = 0;
        for (let m = 0; m < arrJiShu.length; m++) {
            sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
        }

        for (let n = 0; n < arrOuShu.length; n++) {
            sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
        }

        for (let p = 0; p < jishu_child1.length; p++) {
            sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
            sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
        }
        //计算总和
        sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

        //计算luhn值
        let k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
        let luhn = 10 - k;

        if (lastNum == luhn) {
            console.log("验证通过");
            return true;
        } else {
            return false;
        }
    }
}


export {StringUtil, ArrayUtil, BackCardUtil, PhoneUtil};
